package com.test3;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Collections工具类演示
 */
public class CollectionsTest {
    public static void main(String[] args) {
        List<Integer> list = new ArrayList<>();
        list.add(12);
        list.add(34);
        list.add(32);
        list.add(22);
        //sort排序
        Collections.sort(list);
        System.out.println(list);//[12, 22, 32, 34]
        //binarySearch二分查找
        int i = Collections.binarySearch(list,34);
        System.out.println(i);//3
        //copy复制
        List<Integer> dest = new ArrayList<>();
        for (int j = 0; j < list.size(); j++) {
            dest.add(0);
        }
        Collections.copy(dest,list);//直接copy会报错，因为两个集合的大小不一样，所以要用for先把两个集合大小保持一致
        System.out.println(dest);//[12, 22, 32, 34],加了for后copy成功
        //reverse反转
        Collections.reverse(list);
        System.out.println(list);//[34, 32, 22, 12]
        //shuffle 打乱
        Collections.shuffle(list);
        System.out.println(list);//[32, 12, 22, 34]

        //集合转数组
        Integer[] arr = list.toArray(new Integer[0]);
        System.out.println(arr.length);//4
        System.out.println(Arrays.toString(arr));//[12, 22, 34, 32]

        //数组转集合
        String[] name = {"a", "s","d","x"};
        //此集合是一个受限集合，不能添加不能删除
        List<String> list1 = Arrays.asList(name);
        System.out.println(list1);//[a, s, d, x]

        //基本类型数组转集合时，需要修改为包装类型
//        int[] nums = {1,2,3,4,5};
//        List<int[]> list2 = Arrays.asList(nums);
//        System.out.println(list2);//[[I@4554617c]
        Integer[] nums = {1,2,3,4,5};
        List<Integer> list2 = Arrays.asList(nums);
        System.out.println(list2);//[1, 2, 3, 4, 5]
    }
}
